package com.le.book.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;

import com.le.book.Biz.BizException;
import com.le.book.Biz.ReaderBiz;
import com.le.book.util.BookUtil;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class LoginWin {

	protected Shell shell;
	private Text text_name;
	private Text text_pwd;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginWin window = new LoginWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(374, 330);
		shell.setText("图书管理登录界面");
		
		Label label = new Label(shell, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label.setBounds(112, 24, 137, 35);
		label.setText("图书管理登录");
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		label_1.setBounds(50, 88, 76, 26);
		label_1.setText("用户名：");
		
		text_name = new Text(shell, SWT.BORDER);
		text_name.setBounds(130, 88, 159, 26);
		
		Label label_1_1 = new Label(shell, SWT.NONE);
		label_1_1.setText("密    码：");
		label_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		label_1_1.setBounds(50, 144, 76, 26);
		
		text_pwd = new Text(shell, SWT.BORDER);
		text_pwd.setBounds(130, 144, 159, 26);
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ReaderBiz biz = new ReaderBiz();
				String name = text_name.getText().trim();
				String pwd = text_pwd.getText().trim();
				try {
					biz.login(name, pwd);
					// 关闭当前窗口
					LoginWin.this.shell.close();
					// 登陆成功
					new MainWin().open();
					
				} catch (BizException e1) {
					e1.printStackTrace();
					BookUtil.showMessage(shell, "系统提示", e1.getMessage());
				}
				
			}
		});
		button.setBounds(122, 200, 98, 30);
		button.setText("登录");
		
		Button button_1 = new Button(shell, SWT.NONE);
		button_1.setBounds(122, 243, 98, 30);
		button_1.setText("退出");

	}
}
